package com.allen.retrofit2_rxjava2.view.selectImage.view;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * Created by lierdong on 2016/12/5.
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {

    private static final int LOC_NROMAL = 1;
    private static final int LOC_LAST_ROW = 2;
    private static final int LOC_LAST_COLUMN = 3;
    private static final int LOC_LAST_ROW_COLUMN = 4;
    private int decorationSize;

    public GridItemDecoration(int decorationSize) {
        this.decorationSize = decorationSize;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();
        int position = manager.getPosition(view);
        int spanCount = manager.getSpanCount();
        int count = manager.getItemCount();
        int lines = getLines(count, spanCount);
//        Log.d("ItemDecoration", count + ":" + spanCount + ":" + lines);
        //最后一行也是最后一列
//        if (((position + 1) % spanCount == 0) && (position >= (lines - 1) * spanCount)) {
//            Log.d("ItemDecoration", "最后一行也是最后一列:" + position);
//            outRect.set(0, 0, 0, 0);
//            return;
//        }
//        //最后一列
//        if ((position + 1) % spanCount == 0) {
//            Log.d("ItemDecoration", "最后一列:" + position);
//            outRect.set(0, 0, 0, decorationSize);
//            return;
//        }
//        //最后一行
//        if (position >= (lines - 1) * spanCount) {
//            Log.d("ItemDecoration", "最后一行:" + position);
//            outRect.set(0, 0, decorationSize, 0);
//            return;
//        }
        Log.d("ItemDecoration", "普通的:" + position);
        outRect.set(0, 0, decorationSize, decorationSize);
    }


    private int getLines(int count, int spanCount) {
        return count % spanCount == 0 ? count / spanCount : count / spanCount + 1;
    }

    private int getLoc(int count, int lines, int position) {
        return 0;
    }

}
